package com.java2207.group1.controller;

import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.common.R;
import com.java2207.group1.pojo.Equipment;
import com.java2207.group1.service.EquipmentService;
import com.java2207.group1.util.OssUtil;
import com.java2207.group1.vo.equipment.BarVo;
import com.java2207.group1.vo.equipment.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/equ")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    // 查询、分页
    @RequestMapping("/equipmentList")
    public LayuiPageVo<Equipment> equipmentPageData(@RequestParam Map<String, Object> params){
        return equipmentService.equipmentPageData(params);
    }

    // 添加文娱器材信息
    @RequestMapping("/addEquipment")
    public R addEquipment(Equipment equipment){
        int i = equipmentService.addEquipment(equipment);

        if (i == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 根据 id 删除文娱器材信息
    @RequestMapping("/deleteEquipment")
    public R deleteEquipment(int id){
        System.out.println(id);
        int i = equipmentService.deleteEquipment(id);
        if (i == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 修改文娱器材表信息
    @RequestMapping("/updateEquipment")
    public R updateEquipment(Equipment equipment){
        int i = equipmentService.updateEquipment(equipment);
        if (i == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 批量删除
    @GetMapping("/batchEquipment")
    public R batchEquipment(String ids){
        int i = equipmentService.batchEquipment(ids);
        if (i > 0){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 上传图片
    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        if (file != null){
            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            String url = OssUtil.uploadFile(inputStream, originalFilename);

            return R.ok().data("url", url);
        }
        return R.error();
    }

    // 器材价格柱状图
    @RequestMapping("/echartsBar")
    public R echartsBar(){

        List<BarVo> list = equipmentService.setBarData();

        System.out.println(list);

        return R.ok().data("priceBar",list);
    }

    // 器材各器材总量饼图
    @RequestMapping("/echartsPie")
    public R echartsPie(){
        List<PieVo> list = equipmentService.setPieData();

        return R.ok().data("pie", list);
    }

}
